package org.eclipse.iota.ui.parts;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.iota.IotaPlugin;
import org.eclipse.iota.model.Module;
import org.eclipse.iota.ui.embedded.EmbeddedXtextEditor;
import org.eclipse.iota.ui.embedded.EmbeddedXtextEditorModule;
import org.eclipse.iota.xtext.IotaRuntimeModule;
import org.eclipse.iota.xtext.ui.IotaUiModule;
import org.eclipse.iota.xtext.ui.internal.IotaActivator;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IViewSite;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.ui.editor.XtextEditor;
import org.eclipse.xtext.ui.editor.model.IXtextDocument;
import org.eclipse.xtext.ui.shared.SharedStateModule;
import org.eclipse.xtext.util.concurrent.IUnitOfWork;

import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.util.Modules;

/**
 * @author Christian Krause
 * @generated NOT
 */
public class IotaProofView extends ViewPart implements ISelectionListener {

	/**
	 * The ID of the view as specified by the extension.
	 */
	public static final String ID = "org.eclipse.iota.ui.views.IotaProofView";
	
	private Composite container;
	
	private EmbeddedXtextEditor embedded;
	
	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	public void createPartControl(Composite parent) {
		container = new Composite(parent, SWT.NONE);
		container.setLayout(new GridLayout());
		GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
		data.horizontalSpan = 2;
		container.setLayoutData(data);
		
		Injector injector = getDslInjectorForEmbedded();
		embedded = new EmbeddedXtextEditor(container, injector);
		embedded.getViewer().getTextWidget().setBackground(new Color(null, 240, 225, 255));
		embedded.getViewer().setEditable(false);
		embedded.getViewer().getTextWidget().setEnabled(false);
		Font font = JFaceResources.getFont("org.eclipse.jdt.ui.editors.textfont");
		if (font!=null) embedded.getViewer().getTextWidget().setFont(font);
	}

	private Injector getDslInjectorForEmbedded() {
		return Guice.createInjector(
			Modules.override(Modules.override(Modules.override(
				new IotaRuntimeModule()).with(
				new IotaUiModule(IotaActivator.getInstance()))).with(
				new SharedStateModule())).with(
				new EmbeddedXtextEditorModule()));
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.part.ViewPart#init(org.eclipse.ui.IViewSite)
	 */
	@Override
	public void init(IViewSite site) throws PartInitException {
		super.init(site);
		// Add selection listener;
		site.getPage().addSelectionListener(this);
		createActions(site);	
	}
	
	
	private void createActions(IViewSite site) {
		//annotationAction = new Action("Create annotations", Action.AS_CHECK_BOX) {};
		//annotationAction.setImageDescriptor(IotaUIPlugin.getImageDescriptor("icons/annotation.gif"));
		//IToolBarManager mgr = getViewSite().getActionBars().getToolBarManager();
        //mgr.add(annotationAction);
	}
	
	/**
	 * Passing the focus request to the viewer's control.
	 */
	public void setFocus() {
		//viewer.getControl().setFocus();
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.ISelectionListener#selectionChanged(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
	 */
	@Override
	public void selectionChanged(IWorkbenchPart part, ISelection selection) {
		if (part instanceof XtextEditor) {
			XtextEditor editor = (XtextEditor) part;
			IXtextDocument document = (IXtextDocument) editor.getDocument();
			if (document==null) return;
			Module module = document.readOnly(new IUnitOfWork<Module,XtextResource>() {
				public Module exec(XtextResource resource) {
					for (EObject object : resource.getContents()) {
						if (object instanceof Module) {
							return (Module) object;
						}
					}
					return null;
				}
			});
			System.out.println(module);
			
			try {
				String text = module.getDefinitions().get(0).toString();
				this.embedded.update(text);
				this.embedded.getViewer().setSelectedRange(0, 0);
			} catch (Throwable t) {
				IotaPlugin.getInstance().logError("Error initializing embedded Xtext editor", t);
			}
		}		
	}
	
}